/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-06-26 18:55:04
 * @LastEditTime: 2024-07-07 15:10:34
 * @FilePath: /nest_study/web/src/views/system/dict/data/components/info.tsx
 */
/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-06-23 12:37:05
 * @LastEditTime: 2024-06-25 20:58:59
 * @FilePath: /nest_study/web/src/views/system/role/components/info.tsx
 */

import DrawerWrapper from "@/components/drawer_wrapper"
import { Form, Input, InputNumber, message, Select, Switch } from "antd"
import { TDictDataInfo } from "../../index.type";
import { useState } from "react";
import { addInfo, updateInfo } from "@/api/system/dict/dict_data";
import { rules } from "./rules";
import { classList } from "../../dictionary";

const ShowInfo = (props: any) => {
    let { info, parent = '' } = props;
    let initForm: TDictDataInfo = {
        parent,
        label: '',
        value: '',
        class: 'off',
        sort: 1,
        status: '0',
        remark: '',
    }
    const [form] = Form.useForm<any>();
    const [infoForm, _setInfoForm] = useState<TDictDataInfo>({ ...initForm, ...info });
    //提交
    const submit = async () => {
        form.validateFields().then(async values => {
            let info = { ...infoForm, ...values };
            if (info.id) {
                await updateInfo(info);
            } else {
                await addInfo(info);
            }
            message.success('操作成功');
            form.resetFields();
            props.submit();
        }).catch(info => {
            console.log('Validate Failed - ', info);
        });
    }
    return (
        <DrawerWrapper
            title="字典数据信息"
            close={() => props.close()}
            submit={() => submit()}
        >
            <Form
                form={form}
                name="info"
                labelCol={{ span: 5 }}
                wrapperCol={{ span: 17 }}
                initialValues={{ remember: false }}
                onFinish={submit}
                autoComplete="off"
            >
                <Form.Item label="字典类型" initialValue={infoForm.parent} name="parent">
                    <Input disabled placeholder="字典类型" />
                </Form.Item>
                <Form.Item label="字典标签" initialValue={infoForm.label} name="label" rules={rules.label}>
                    <Input allowClear placeholder="请输入字典标签" />
                </Form.Item>
                <Form.Item label="字典键值" initialValue={infoForm.value} name="value" rules={rules.value}>
                    <Input allowClear placeholder="请输入字典键值" />
                </Form.Item>
                <Form.Item label="样式属性" initialValue={infoForm.class} name="class">
                    <Select
                        style={{ width: '100%' }}
                        placeholder="请选择样式属性"
                        options={[...classList]}
                    />
                </Form.Item>
                <Form.Item label="状态" initialValue={infoForm.status} name="status">
                    <Switch checked={infoForm.status == '0' ? true : false} onChange={(e) => { form.setFieldValue('status', e ? '0' : '1'); _setInfoForm({ ...infoForm, status: e ? '0' : '1' }) }} checkedChildren="启用" unCheckedChildren="关闭" />
                </Form.Item>
                <Form.Item label="排序" initialValue={infoForm.sort} name="sort" >
                    <InputNumber min={0} precision={0} style={{ width: '100%' }} placeholder="请输入排序" />
                </Form.Item>
                <Form.Item label="备注" initialValue={infoForm.remark} name="remark">
                    <Input.TextArea placeholder="请输入备注" />
                </Form.Item>
            </Form>
        </DrawerWrapper>
    )
}

export default ShowInfo